//
//  MeetingFooterView.swift
//  codelabs
//
//  Created by 雷雷 on 26/05/2025.
//

import SwiftUI
import TimerKit

struct MeetingFooterView: View {
    let speakers:[ScrumTimer.Speaker]
    
    var startAction:()->Void
    
    var skipAction:()->Void
    
    var doneAction:()->Void
    
    @Binding var doneMeeting:Bool
    
    
    private var speakerNumber:Int?{
        guard let index = speakers.firstIndex(where: { !$0.isCompleted }) else { return nil }
        return index + 1
    }
    
    private var isLaskSpeaker:Bool{
        return speakers.dropLast().allSatisfy { $0.isCompleted }
    }
    
    private var speakerText: String {
        guard let speakerNumber = speakerNumber else { return "No more speakers" }
        return "Speaker \(speakerNumber) of \(speakers.count)"
    }
    
    var body: some View {
        VStack {
            HStack{
                if isLaskSpeaker {
                    
                    if doneMeeting{
                        Text("All Speakers Done")
                    }else{
                        Text("Last Speaker")
                    }
                    
                    Spacer()
                    
                }else{
                    Text("Speaker \(speakerNumber ?? 0) of \(speakers.count)")
                    
                    Spacer()
                    
                    Button(action:skipAction){
                        Image(systemName: "forward.fill")
                    }
                    .accessibilityLabel("Next speaker")
                }
                
                
                Button(action:{
                    if doneMeeting{
                        startAction()
                    }else{
                        doneAction()
                    }
                    
                    // Action before state changed.
                    doneMeeting.toggle()
                    
                }){
                    if doneMeeting{
                        Image(systemName: "play.fill")
                            .accessibilityLabel("Restart Meeting")
                    }else{
                        Image(systemName: "stop.fill")
                            .accessibilityLabel("End meeting")
                    }
                    
                }
                .padding(.leading,24)
                
            }
        }
        .padding([.bottom,.horizontal])
    }
}

#Preview {
    @Previewable var speakers = DailyScrum.sampleData[0].attendees
        .map{
            $0.name
        }
        .map{
            ScrumTimer.Speaker.init(name: $0, isCompleted: false)
        }
    
    MeetingFooterView(speakers: speakers,startAction:{},skipAction:{},doneAction:{},doneMeeting: .constant(false))
}
